package com.mcms.service.dw;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.ralasafe.user.User;

import com.mcms.util.CalendarUtil;
import com.mcms.util.DBHelper;
import com.mcms.util.GetParamsUtil;
import com.mcms.util.McmsApiConfig;

/**
 * 将客户基本信息插入数据库
 * 
 * @since 2012-7-14
 * @see
 * version 1.0.0 2012-7-14
 */
public class AddSubscribeDoctorInfoService {

	public static boolean execute(HttpServletRequest request,String uuid){
		
		boolean isSuc=true;
		
		//先将该customId下的所有预约就诊信息设置为作废状态
		boolean isUpdate = updateStatus(uuid);
		
		//状态修改成功，则插入新的预约就诊信息
		if(isUpdate){
			isSuc = addSubscribeDoctorInfo(request,uuid);
		}else{
			isSuc = false;
		}
		
		return isSuc;
	}
	
	/**
	 * 先将该customId下的其他预约就诊信息设置为作废状态
	 * 
	 * @param customId
	 * @return
	 */
	public static boolean updateStatus(String customId){
		boolean isUpdate = true;
		String sqlStr = " update mcms_subscribe_doctor t set t.is_cancel = '1' where t.custom_id = '"+customId+"'";
		
		try {
			DBHelper dbh = DBHelper.getInstance();
			int i = dbh.execute(sqlStr);
			if(i < 0){
				isUpdate = false;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return isUpdate;
	}
	
	public static boolean addSubscribeDoctorInfo(HttpServletRequest request,String uuid){
		boolean isSuc=true;
		
		//获取参数
		List paramsList = new ArrayList();
		String id = UUID.randomUUID().toString();
		Map mcmsSession = (Map)request.getSession().getAttribute("mcmsSession");
		User user = (User)mcmsSession.get("mcmsUser");
		String userId = user.getId().toString();
		paramsList.add(id);//预约就诊号
		paramsList.add(request.getParameter("subscribe_doctor_time").toString());//预约就诊时间
		paramsList.add(uuid);//客户号
		paramsList.add(request.getParameter("remark").toString());//备注
		paramsList.add(CalendarUtil.getCurrentDateTime());//创建时间
		paramsList.add(userId);//创建人
		paramsList.add("1");//是否作废,默认为否：1
		
		//参数入库
		String sqlStr = McmsApiConfig.get("sql_addCustom_dw_subscribe");
		try {
			DBHelper dbh = DBHelper.getInstance();
			int i = dbh.execute(sqlStr, paramsList);
//			List list = dbh.query(sqlStr, paramsList);
			if(i==0){
				isSuc = false;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return isSuc;
	}
}
